##
rm(list=ls())
gc()


library(data.table)
library(ggplot2)
library(dplyr)
library(stringr)

files = paste0('results/',c(
                                        'future-results-2012-2016-Asian-subset.Rdata',
                            
                                        'future-results-2012-2016-Black-subset.Rdata',
                                     
                                        'future-results-2012-2016-Hispanic-subset.Rdata',
                                    
                                        'future-results-2012-2016-White-subset.Rdata'
                                        ))




results = rbindlist(lapply(files, FUN=function(file){

  
  load(file)
  
  models = ls()[grepl('Model',ls())]
  
  l = rbindlist(lapply(models, FUN=function(x){
   # print(x)
    m = get(x)
    out = as.data.table(m$coefficient)
    out[,Covariate:=rownames(m$coefficient)]
    out[,Model:=x]
    out[,N:=m$N]
    out[,`R-Squared`:=m$r.squared]
    out[,`Adjusted R-Squared`:=m$adj.r.squared]
    out[,Sigma:=m$sigma]
    out[,FStat:=m$fstat]
    if('Cluster s.e.'%in%names(out)){
      out[,SE:=`Cluster s.e.`]
      out[,SE.type:='Cluster']
      out[,`Cluster s.e.`:=NULL]
    } else {
      out[,SE:=`Robust s.e`]
      out[,SE.type:='Robust']
      out[,`Robust s.e`:=NULL]
    }


return(out)

  }))
  
  l[grepl('2008',file),Year1:='2008']
  l[grepl('2008',file),Year2:='2012']
l[grepl('2020',file),Year1:='2016']
l[grepl('2020',file),Year2:='2020'] 
l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
l[,file:=file]

return(l)
}))


results = results[Covariate %in% c('InteractionWhite:DemSpExpDiff_nohh','InteractionWhite:RepSpExpDiff_nohh',
                                   'InteractionNon-White:DemSpExpDiff_nohh','InteractionNon-White:RepSpExpDiff_nohh',
                                   
                                   'InteractionBlack:DemSpExpDiff_nohh','InteractionBlack:RepSpExpDiff_nohh',
                                   'InteractionNon-Black:DemSpExpDiff_nohh','InteractionNon-Black:RepSpExpDiff_nohh',
                                   
                                   
                                   'InteractionHispanic:DemSpExpDiff_nohh','InteractionHispanic:RepSpExpDiff_nohh',
                                   'InteractionNon-Hispanic:DemSpExpDiff_nohh','InteractionNon-Hispanic:RepSpExpDiff_nohh',
                                   
                                   
                                   'InteractionAsian:DemSpExpDiff_nohh','InteractionAsian:RepSpExpDiff_nohh',
                                   'InteractionNon-Asian:DemSpExpDiff_nohh','InteractionNon-Asian:RepSpExpDiff_nohh'
                                   )]

results[!grepl('Non-',Covariate),Type:='Ingroup']
results[grepl('Non-',Covariate),Type:='Outgroup']

results[grepl('White',file),Race:='White']
results[grepl('Asian',file),Race:='Asian']
results[grepl('Black',file),Race:='Black']
results[grepl('Hispanic',file),Race:='Hispanic']

results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']
results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

results[grepl('Dems',Model),Subset:='Democrats']
results[grepl('Reps',Model),Subset:='Republicans']
results[grepl('Oths',Model),Subset:='Non-partisans']
results[,Years:=paste0(Year1,'-',Year2)]

colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")


g = ggplot(results %>%
             mutate(Race = paste0(Race,' partisan exposure')), aes(y = Estimate, x = Subset, color = Subset, shape = Type))+
  geom_point( size = 5, position = position_dodge(width = 0.75))+
  geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
  geom_hline(yintercept = 0, linetype = 'dashed')+
  coord_flip()+
  theme_bw()+
  scale_color_manual(values = colors)+
  ylab('Effect of partisan exposure')+
  xlab('Original party')+
  guides(color = 'none')+
  theme(text = element_text(size = 16, family = 'serif'),panel.spacing = unit(.5, "lines"), strip.text.y = element_text(size = 10, angle = 0), legend.title=element_blank(),legend.position = 'bottom')+
  facet_grid(Exposure.Type~Race, scales = 'free_x')
ggsave(plot = g,filename = 'figures/FigS7.png',dpi=300, width = 12, height = 5, units = 'in')





